# -*- coding: utf-8 -*-
import scrapy
from sina.items import DangdangItem

class DangdangSpider(scrapy.Spider):
    name = 'dangdang'
    allowed_domains = ['search.dangdang.com']
    start_urls = ['http://search.dangdang.com/?key=python&act=input&page_index=1']
    currentNum=0# 爬取的第几条
    def parse(self, response):
        '''解析获取所有页码'''
        maxPage = 1 # 最大页码
        pagehtmllist=response.css("div.paging ul li")
        for p in pagehtmllist:
            nums=p.re("<li><a.*?>([0-9]+)</a></li>")
            if nums:
                maxPage=int(nums[0])
        # 遍历页码，访问每页数据
        for page in range(1,55):
            max_url="http://search.dangdang.com/?key=python&act=input&page_index=" + str(page)
            url=response.urljoin(max_url)
            yield scrapy.Request(url=url,callback=self.parse_page)
    def parse_page(self,response):
        '''解析当前页面的数据'''
        # 获取图书列表
        bookList = response.css("ul.bigimg li")
        # print(len(bookList))
        for book in bookList:
            self.currentNum+=1
            # print(self.currentNum)
            item = DangdangItem()
            item["title"]=""
            item["bookUrl"] = ""
            item["price"] = 0
            item["price_pre"] = 0
            item["discount"] = 0
            item["author"] = ""
            item["date"] = ""
            item["place"] = ""
            item["num"] = 0
            item["imgName"] = ""
            item["imgUrl"] = ""
            item["intro"] = ""
            item["dataindex"] = 0
            title = book.css("p.name a::attr(title)")  # 标题
            if title:
                item["title"]=title.extract_first()
            bookurls = book.css("p.name a::attr(href)")  # 图书链接
            if bookurls:
                item["bookUrl"]=bookurls.extract_first()
            prices= book.re('<span class="search_now_price">¥([0-9\.]+)</span>')  # 价格
            if prices:
                item["price"] =prices[0]
            price_pres = book.re('<span class="search_pre_price">¥([0-9\.]+)</span>')  # 原价
            if price_pres:
                item["price_pre"]=price_pres[0]
            discounts = book.re('<span class="search_discount">\s*\(([0-9\.]+)折\)\s*</span>')  # 折扣
            if discounts:
                item["discount"]=discounts[0]
            authors = book.css("p.search_book_author span:first-child a:first-child::attr(title)")  # 作者
            if authors:
                item["author"]=authors.extract_first()
            dates = book.css("p.search_book_author span:nth-child(2)::text")  # 出版日期
            if dates:
                item["date"] = dates.extract_first().lstrip(" /")
            places = book.css("p.search_book_author span:nth-child(3) a::text")  # 出版社
            if places:
                item["place"]=places.extract_first()
            nums = book.re('<p class="search_star_line">.*?<a.*?>([0-9]+)条评论</a>.*?</p>')  # 评论数
            if nums:
                item["num"]=nums[0]
            temp_img=book.css("a.pic img::attr(data-original)").extract_first()  # 图片
            if not temp_img:
                item["imgUrl"] = book.css("a.pic img::attr(src)").extract_first()  # 图片
            else:
                item["imgUrl"]=temp_img
            item["imgName"] = ""  # 图片名称
            intros = book.css("p.detail::text")  # 简介
            if intros:
                item["intro"]=intros.extract_first()
            item["dataindex"]=self.currentNum# 记录第几条数据
            # print(item)
            yield item